package com.chanjiedata.changbs.module.mine;

/**
 * 文件名 : RenZhenActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 18:17
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.bean.UserBean;
import com.chanjiedata.changbs.help.GlideHelper;
import com.chanjiedata.changbs.help.ImageHelper;
import com.chanjiedata.changbs.help.PermissionHelper;
import com.chanjiedata.changbs.help.UserHelper;
import com.chanjiedata.changbs.module.main.MainActivity;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.LoginOuterClass;
import com.chanjiedata.changbs.proto.MemberOuterClass;
import com.chanjiedata.changbs.proto.PBPublicOuterClass;
import com.chanjiedata.changbs.utlis.GsonUtils;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.google.protobuf.InvalidProtocolBufferException;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RenZhenActivity extends BaseActivity {
    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_card)
    EditText etCard;
    @BindView(R.id.iv_qr_home)
    ImageView ivQrHome;
    @BindView(R.id.iv_qr_back)
    ImageView ivQrBack;
    @BindView(R.id.tv_submit)
    Button tvSubmit;
    @BindView(R.id.iv_qr_shou_chi)
    ImageView ivQrShouChi;
    private int selectImageType = 0;
    private String compressPath;  // 身份证首页图片路径
    private String compressPathTwo; // 身份证背面图片路径
    private String compressPathThree; // 身份证背面图片路径
    private String UploadPath;  // 上传身份证首页图片路径
    private String UploadPathTwo; // 上传身份证背面图片路径
    private String UploadPathThree; // 上传身份证背面图片路径


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ren_zhen);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        tvTitle.setText("实名认证");

    }


    @OnClick({R.id.back_tv, R.id.iv_qr_home, R.id.iv_qr_back, R.id.tv_submit, R.id.iv_qr_shou_chi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_tv:
                finish();
                break;
            case R.id.iv_qr_home:
                selectImageType = 0;
                PermissionHelper.getInstance().getPermission(this, new PermissionHelper.PermissionCallBack() {
                    @Override
                    public void onSuccess() {
                        ImageHelper.showImageChoose(RenZhenActivity.this);
                    }

                    @Override
                    public void onFail() {
                        showToast("请先允许权限再使用此功能");
                    }
                }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                break;
            case R.id.iv_qr_back:
                selectImageType = 1;
                PermissionHelper.getInstance().getPermission(this, new PermissionHelper.PermissionCallBack() {
                    @Override
                    public void onSuccess() {
                        ImageHelper.showImageChoose(RenZhenActivity.this);
                    }

                    @Override
                    public void onFail() {
                        showToast("请先允许权限再使用此功能");
                    }
                }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                break;
            case R.id.iv_qr_shou_chi:
                selectImageType = 2;
                PermissionHelper.getInstance().getPermission(this, new PermissionHelper.PermissionCallBack() {
                    @Override
                    public void onSuccess() {
                        ImageHelper.showImageChoose(RenZhenActivity.this);
                    }

                    @Override
                    public void onFail() {
                        showToast("请先允许权限再使用此功能");
                    }
                }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                break;
            case R.id.tv_submit:
                submit();
                break;
        }
    }

    private void submit() {
        String name = etName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast("请输入名称");
            return;
        }
        String card = etCard.getText().toString();
        if (TextUtils.isEmpty(card)) {
            showToast("请输入银行卡号");
            return;
        }
        if (TextUtils.isEmpty(compressPath)) {
            showToast("请拍摄身份证人像页");
            return;
        }
        if (TextUtils.isEmpty(compressPathTwo)) {
            showToast("请拍摄身份证国徽页");
            return;
        }
        if (TextUtils.isEmpty(compressPathThree)) {
            showToast("请拍摄手持身份证照片");
            return;
        }
        upLoadImage();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {

            List<LocalMedia> localMedias = PictureSelector.obtainMultipleResult(data);
            Log.e("wang", "onActivityResult: " +  localMedias.get(0).getCompressPath() );
            if (selectImageType == 0) {
                compressPath = localMedias.get(0).getCompressPath();
                GlideHelper.INSTANCE.loadImage(ivQrHome, compressPath);
            } else if (selectImageType == 1) {
                compressPathTwo = localMedias.get(0).getCompressPath();
                GlideHelper.INSTANCE.loadImage(ivQrBack, compressPathTwo);
            } else if (selectImageType == 2) {
                compressPathThree = localMedias.get(0).getCompressPath();
                GlideHelper.INSTANCE.loadImage(ivQrShouChi, compressPathThree);
            }
        }
    }

    private void upLoadImage() {
        showProgress();
        ImageHelper.upLoadImage(compressPath, new ImageHelper.OnUpLoadImageCallBack() {
            @Override
            public void onSuccess(String images) throws InvalidProtocolBufferException {
                String[] temp = images.split(",");
                byte[] bytes = new byte[temp.length];
                for (int i = 0; i < temp.length; i++) {
                    bytes[i] = (byte) Integer.parseInt(temp[i]);
                }
                UploadPath = MemberOuterClass.img_upload.parseFrom(bytes).getImg();
                ImageHelper.upLoadImage(compressPathTwo, new ImageHelper.OnUpLoadImageCallBack() {
                    @Override
                    public void onSuccess(String images) throws InvalidProtocolBufferException {
                        String[] temp = images.split(",");
                        byte[] bytes = new byte[temp.length];
                        for (int i = 0; i < temp.length; i++) {
                            bytes[i] = (byte) Integer.parseInt(temp[i]);
                        }
                        UploadPathTwo = MemberOuterClass.img_upload.parseFrom(bytes).getImg();
                        ImageHelper.upLoadImage(compressPathThree, new ImageHelper.OnUpLoadImageCallBack() {
                            @Override
                            public void onSuccess(String images) throws InvalidProtocolBufferException {
                                String[] temp = images.split(",");
                                byte[] bytes = new byte[temp.length];
                                for (int i = 0; i < temp.length; i++) {
                                    bytes[i] = (byte) Integer.parseInt(temp[i]);
                                }
                                UploadPathThree = MemberOuterClass.img_upload.parseFrom(bytes).getImg();
                                indent();
                            }

                            @Override
                            public void onFail(String error) {
                                hideProgress();
                                showToast(error);
                            }
                        });
                    }

                    @Override
                    public void onFail(String error) {
                        hideProgress();
                        showToast(error);
                    }
                });
            }

            @Override
            public void onFail(String error) {
                hideProgress();
                showToast(error);
            }
        });
    }


    private void indent() {
        MemberOuterClass.member_approve.Builder builders = MemberOuterClass.member_approve.newBuilder();
        builders.setTruename(etName.getText().toString()).setIdcard(etCard.getText().toString())
                .setPicBefore(UploadPath).setPicAfter(UploadPathTwo).setPicCatch(UploadPathThree);
        showProgress();
        NetworkManager.INSTANCE.post(Apis.member_approve, builders.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) {

            }
            @Override
            public void onError(int errorCode, String errorMessage) {
                hideProgress();
                showToast(errorMessage);
                if (errorCode == 200) {
                    startActivity(MainActivity.class);
                    finish();
                }
            }
        });
    }
}
